<?php

namespace app\model;

use think\Model;

class Product extends Model
{
    // 允许写入的字段
    protected $allowFields = [
        'user_id',
        'product_description',
        'product_img',
        'price',
        'location',
        'tel',
        'shipping_method'
    ];

    public static function publishProduct($userId,$productDescription,$ProductImg,$price,$location,$tel,$shippingMethod)
    {

        // 商品信息
        $data = [
            "user_id" => $userId,
            "product_description" => $productDescription,
            "product_img" => $ProductImg,
            "price" => $price,
            "location" => $location,
            "tel" => $tel,
            "shipping_method" => $shippingMethod,
        ];

        // 新增商品
        $product = self::create($data);

        // 新增商品失败
        if (!$product) {
            throw new \Exception('发布失败');
        }

        // 返回商品信息
        return $product;

    }
}